﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aulac.Rsc.Bss;
using Aulac.Rsc.Bss.Tags;
using Aulac.Global.Common;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormList_Dm_Provider : Aulac.Client.Controls.Forms.Bss.FormEmsListItemsData
    {
        private FormDlgProvider mFormDlgProvider = null;
        private string mTitleName = "";
        public FormList_Dm_Provider()
        {
            InitializeComponent();
            grdList.DataSource = new Aulac.Client.Data.Bss.DataSet.Catalog.Dm_ProviderDataTable();

            string strTitleName = this.GetTileDmTypesName(Dm_TbName.Dm_Provider);
            if (!ValueHelper.isEmpty(strTitleName))
            {
                mTitleName = strTitleName;
                this.Text = Aulac.Global.Common.StringHelper.ReplaceIndex(strTitleName.ToLowerInvariant(), strTitleName.Substring(0, 1).ToLower(), strTitleName.Substring(0, 1).ToUpper(), 0);
            }
        }
       
        #region override Methods
        public override void OnFormLoad()
        {
            base.OnFormLoad();
            grdList.ClearColumsShow();
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Mã nhà cung cấp", ColName = tagItems.Provider.ProviderID, DisplayIndex = 0 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Tên nhà cung cấp", ColName = tagItems.Provider.ProviderName, DisplayIndex = 1 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Địa chỉ nhà cung cấp", ColName = tagItems.Provider.ProviderAddr, DisplayIndex = 2 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Số điện thoại nhà cung cấp", ColName = tagItems.Provider.ProviderPhone, DisplayIndex = 3 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Email", ColName = tagItems.Provider.ProviderEmail, DisplayIndex = 4 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Đang hoạt động", ColName = tagItems.Provider.IsActive, DisplayIndex = 5 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Người đại diện", ColName = tagItems.Provider.ProviderDelegate, DisplayIndex = 6 });
            grdList.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ghi chú", ColName = tagItems.Provider.Note, DisplayIndex = 7 });            
           grdList.Columns[tagItems.Provider.ProviderName].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        protected override void OnSetDataSource(out object pDataSource, out string pTableName)
        {
            pDataSource = null;
            pTableName = TableNames.Dm_Provider;
        }
        protected override void OnAddParamsQuery()
        {
            //AddParamQuery(tagItems.Countries.IsActive, tagItems.Countries.IsActive, 1);// gia tri can truy van co isactive = 1;
        }

        //protected override string OnCreateQueryString()
        //{
        //    string sqlQuery = string.Format("SELECT C.{0}, CGrp.{1}, CT.{2}", "*", tagItems.CapAssGrp.CapAssGrpID, tagItems.CapAssType.CapAssTypeID);
        //    sqlQuery += string.Format(" FROM {0} C INNER JOIN", TableNames.Dm_CapAss);
        //    sqlQuery += string.Format(" {0} CGrp ON C.{1} = CGrp.{2}", TableNames.Dm_CapAssGrp, tagItems.CapAss.CapAssGrpPrkID, tagItems.CapAssGrp.CapAssGrpPrkID);
        //    sqlQuery += string.Format(" INNER JOIN {0} CT ON C.{1} = CT.{2}", TableNames.Dm_CapAssType, tagItems.CapAss.CapAssTypePrkID, tagItems.CapAssType.CapAssTypePrkID);
        //    return sqlQuery;
        //}

        protected override void OnListShowDetailVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.ViewMode);
        }
        protected override void OnListAddNewVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.NewMode);
            mFormDlgProvider.ShowDialog();
        }
        protected override void OnListEditVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.EditMode);
        }
        protected override void OnListDelDataVoucher()
        {
            ProcessFormCmd(Aulac.Global.Enums.FormModes.DelMode);
        }
        #endregion
        #region Methods
        private void ProcessFormCmd(Aulac.Global.Enums.FormModes pFormmode)
        {
            try
            {
                if (mFormDlgProvider == null)
                {
                    mFormDlgProvider = new FormDlgProvider();
                    FormDataItemsCmd = mFormDlgProvider;
                    mFormDlgProvider.ExitComplated += new EventHandler(FrmDlgProvider_ExitComplete);                                
                }                
                if (!ValueHelper.isEmpty(mTitleName))
                    mFormDlgProvider.TitleName = mTitleName;
                mFormDlgProvider.ProviderPrkID = grdList.GetValue(grdList.SelectedGridRowView, tagItems.Provider.ProviderPrkID);
                mFormDlgProvider.FormMode = pFormmode;

                if (pFormmode == Aulac.Global.Enums.FormModes.ViewMode)
                {
                    mFormDlgProvider.ShowDetailVoucher();
                }
                else if (pFormmode == Aulac.Global.Enums.FormModes.NewMode)
                {
                    mFormDlgProvider.ProviderPrkID = "";
                    mFormDlgProvider.AddNewVoucher();
                }
                else if (pFormmode == Aulac.Global.Enums.FormModes.EditMode)
                {
                    mFormDlgProvider.EditVoucher();
                }
                else if (pFormmode == Aulac.Global.Enums.FormModes.DelMode)
                {
                    mFormDlgProvider.DeleteProvider();
                    this.RefreshList();
                    return;
                }
                mFormDlgProvider.ShowDialog();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        private void FrmDlgProvider_ExitComplete(object sender, EventArgs e)
        {
            this.CancelTransaction();
            this.grdList.ClearDataContainer();
            this.RefreshList();
        }
        #endregion
    }
}
